import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import java.lang.*;
import java.util.*;
import java.io.*;

public class Insert extends JFrame{

 Container contentpane;
 
 //==========================================================
 String File_path = "D:\\database\\DB.txt";
 String Inser_temp="";
 String Insert="";
 String title_String="";
 String person_String="";
 String comperny_String="";
 String year_String="";
 String stock_String="";
 String price_String="";
 int I_temp;
 char[] load_temp = new char[20000];
 //==========================================================
 FileWriter save;
 FileInputStream load;
 //==========================================================
 
 JLabel label = new JLabel("1. 도서검색");
 JButton button = new JButton("검색");
 
 JLabel label2 = new JLabel("2. 도서등록");
 JButton button2 = new JButton("등록");
 
 JLabel label3 = new JLabel("3. 도서삭제");
 JButton button3 = new JButton("삭제");


 insert_dialog ID = new insert_dialog(this,"등록");

 
 
 public Insert()
 {
  setTitle("연습1");
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  contentpane = getContentPane();
  contentpane.setLayout(null);
  
  //======================================
  contentpane.add(label);
  label.setSize(150,20);
  label.setLocation(10,10);
  
  contentpane.add(button);
  button.setSize(150,20);
  button.setLocation(10,30);
  button.addActionListener(new button_event());
  //======================================
  
  //======================================
  contentpane.add(label2);
  label2.setSize(150,20);
  label2.setLocation(10,50);
  
  contentpane.add(button2);
  button2.setSize(150,20);
  button2.setLocation(10,70);
  button2.addActionListener(new button_event());
  //======================================
  
  //======================================
  contentpane.add(label3);
  label3.setSize(150,20);
  label3.setLocation(10,90);
    
  contentpane.add(button3);
  button3.setSize(150,20);
  button3.setLocation(10,110);
  button3.addActionListener(new button_event());
  //======================================
  
  setSize(200,200);
  setVisible(true);
 }
 
 public class button_event implements ActionListener
 {
  public void actionPerformed(ActionEvent e)
  {
   JButton temp = (JButton)e.getSource();
   if(temp.getText().equals("검색"))
   {
    
   }
   else if(temp.getText().equals("등록"))
   {
    ID.setVisible(true);
   }
   else if(temp.getText().equals("삭제"))
   {
    
   }
  }
 }
 
 public class insert_dialog extends JDialog
 {
  JLabel line = new JLabel("도서를 등록하시오.");
  
  JLabel title_S = new JLabel("도서명  :");
  JTextField title_TitleField_S = new JTextField(15);
  
  JLabel person_S = new JLabel("저자명  :");
  JTextField person_TitleField_S = new JTextField(15);
  
  JLabel comperny_S = new JLabel("출판사명 :");
  JTextField comperny_TitleField_S = new JTextField(15);
  
  JLabel year_S = new JLabel("출판년도 :");
  JTextField year_TitleField_S = new JTextField(15);
  
  JLabel stock_S = new JLabel("입고권수 :");
  JTextField stock_TitleField_S = new JTextField(15);
  
  JLabel price_S = new JLabel("출고가  :");
  JTextField price_TitleField_S = new JTextField(15);
  
  JButton next = new JButton("등록");
  JButton complet = new JButton("완료");
  
 
  
  public insert_dialog(JFrame frame,String name)
  {
   super(frame,name);
   setTitle("도서등록 목록");
   setLayout(null);
   
   add(line);
   line.setSize(130,20);
   line.setLocation(10,10);
   //=======================================
   add(title_S);
   title_S.setSize(50,20);
   title_S.setLocation(20,40);
   
   add(title_TitleField_S);
   title_TitleField_S.setSize(150,20);
   title_TitleField_S.setLocation(70,40);
   //=======================================
   
   //=======================================
   add(person_S);
   person_S.setSize(50,20);
   person_S.setLocation(20,70);
   
   add(person_TitleField_S);
   person_TitleField_S.setSize(150,20);
   person_TitleField_S.setLocation(70,70);
   //=======================================
   
   //=======================================
   add(comperny_S);
   comperny_S.setSize(60,20);
   comperny_S.setLocation(10,100);
   
   add(comperny_TitleField_S);
   comperny_TitleField_S.setSize(150,20);
   comperny_TitleField_S.setLocation(70,100);
   //=======================================
   
   //=======================================
   add(year_S);
   year_S.setSize(60,20);
   year_S.setLocation(10,130);
   
   add(year_TitleField_S);
   year_TitleField_S.setSize(150,20);
   year_TitleField_S.setLocation(70,130);
   //=======================================
   
   //=======================================
   add(stock_S);
   stock_S.setSize(60,20);
   stock_S.setLocation(10,160);
   
   add(stock_TitleField_S);
   stock_TitleField_S.setSize(150,20);
   stock_TitleField_S.setLocation(70,160);
   //=======================================
   
   //=======================================
   add(price_S);
   price_S.setSize(60,20);
   price_S.setLocation(20,190);
   
   add(price_TitleField_S);
   price_TitleField_S.setSize(150,20);
   price_TitleField_S.setLocation(70,190);
   //=======================================
   
   //=======================================
   add(next);
   next.setSize(90,20);
   next.setLocation(20,220);
   next.addActionListener(new ActionListener(){
    
    public void actionPerformed(ActionEvent e)
    {
     JButton temp = (JButton)e.getSource();
     if(temp.getText().equals("등록"));
     {
      //======================================================
       title_String=title_TitleField_S.getText();
       person_String=person_TitleField_S.getText();
       comperny_String=comperny_TitleField_S.getText();
       year_String=year_TitleField_S.getText();
       stock_String=stock_TitleField_S.getText();
       price_String=price_TitleField_S.getText();
       //=====================================================
       
       Inser_temp=Inser_temp.concat("도서명: "+title_String+" ");
       Inser_temp=Inser_temp.concat("저자명: "+person_String+" ");
       Inser_temp=Inser_temp.concat("출판사: "+comperny_String+" ");
       Inser_temp=Inser_temp.concat("출판년도: "+year_String+" ");
       Inser_temp=Inser_temp.concat("재고수량: "+stock_String+" ");
       Inser_temp=Inser_temp.concat("출고가: "+price_String+" "+"\r\n"); 
       System.out.println(Inser_temp);
       
       title_TitleField_S.setText("");
       person_TitleField_S.setText("");
       comperny_TitleField_S.setText("");
       year_TitleField_S.setText("");
       stock_TitleField_S.setText("");
       price_TitleField_S.setText("");
     
     }
    }
   });
   
   //=======================================
   
   //=======================================
   add(complet);
   complet.setSize(90,20);
   complet.setLocation(120,220);
   complet.addActionListener(new ActionListener(){
    
    public void actionPerformed(ActionEvent e)
    {
     JButton temp1 = (JButton)e.getSource();
     if(temp1.getText().equals("완료"));
     { 
      try{
       load = new FileInputStream(File_path);
       while((I_temp=load.read())!=-1)
       {
    	
        Insert=Insert.concat(Integer.toString(I_temp));
       }
       load.close();
      
      }catch(IOException q)
      {
       JOptionPane.showMessageDialog(null, "DB도서 등록 실패","경고",JOptionPane.WARNING_MESSAGE);
      }
      
      try{
    	
        Insert=Insert.concat(Inser_temp);
        save = new FileWriter(File_path);
        save.write(Insert);
        save.close();
      }catch(IOException t)
      {
       JOptionPane.showMessageDialog(null, "DB도서 등록 실패","경고",JOptionPane.WARNING_MESSAGE);
      }
      
      title_TitleField_S.setText("");
      person_TitleField_S.setText("");
      comperny_TitleField_S.setText("");
      year_TitleField_S.setText("");
      stock_TitleField_S.setText("");
      price_TitleField_S.setText("");
      
      Inser_temp="";
      
      JOptionPane.showMessageDialog(null, "등록이 완료 되었습니다.");
      setVisible(false);
     }
    }
   });
   //=======================================
   
   setSize(250,300);
   setVisible(false);
   
  }
 }
 
 public static void main(String[]args)
 {
  new Insert();
 }
} 
